package TUILayer;
import ControlLayer.*;

public class CopyUI
{

    private DVDCtr dvdCtr;
    private InputParser inputParser;
    private int id = 0;

    public CopyUI(DVDCtr dvdCtr, InputParser inputParser)
    {
        this.dvdCtr = dvdCtr;
        this.inputParser = inputParser;
    }

    public void start()
    {
        id = inputParser.getInt("\f ***DVDInfoMenu ***\n\nPlease input the id of the DVD: ");
        if(dvdCtr.getDVD(id) != null)
        {
            int choise = 0;
            boolean exit = false;
            while(choise < 1 || choise > 6 || !exit)
            {
                choise = inputParser.getInt("\f***DVDInfo***\n" + dvdCtr.getDVDInfo(id) + "\n*** CopyMenu ***\n\n1. Select Copy\n2. Add Copy\n3. Delete Copy\n4. Update Copy\n5. List all Copys\n6. Return\n\nMake your choise: ");
                if(choise == 1)
                {
                    showInfo();
                }
                else if(choise == 2)
                {
                    createCopy();
                }
                else if(choise == 3)
                {
                    deleteCopy();
                }
                else if(choise == 4)
                {
                    updateCopy();
                }
                else if(choise == 5)
                {
                    listAllCopies();
                }
                else if(choise == 6)
                {
                    exit = true;
                }

            }
        }
        else
        {
            System.out.println("\f ***DVDInfoMenu ***\n");
            System.out.println("Error: DVD not found");
            inputParser.printReturn();
        }
    }

    private void createCopy()
    {
        long serial = inputParser.getLong("\f ***CreateCopyMenu ***\n\nPlease input the serial number of the new copy: ");
        if(dvdCtr.getCopy(id, serial) == null)
        {
            long purchaseDate = inputParser.getDate("\f ***CreateCopyMenu ***\n\nPlease input the purchase date (dd/mm/yyyy) of the new copy: ");
            int purchasePrice = inputParser.getInt("\f ***CreateCopyMenu ***\n\nPlease input the purchase price of the new copy: ");
            dvdCtr.createCopy(id, serial, purchaseDate, purchasePrice);
            System.out.println("\f ***CreateCopyMenu ***\n\nCopy added.");
        }
        else
        {
            System.out.println("\f ***CreateCopyMenu ***\n\nError: Id already exists in the Copy Collection");
        }
        inputParser.printReturn();
    }

    private void showInfo()
    {        
        long serial = inputParser.getLong("\f ***CopyInfoMenu ***\n\nPlease input the serial number of the Copy: ");
        System.out.println("\f ***CopyInfoMenu ***\n");
        if(dvdCtr.getCopy(id, serial) != null)
        {
            dvdCtr.printCopyInfo(id, serial);
        }
        else
        {
            System.out.println("Error: Copy not found");
        }
        inputParser.printReturn();
    }

    private void deleteCopy()
    {
        long serial = inputParser.getLong("\f ***DeleteCopyMenu ***\n\nPlease input the serial number of the Copy: ");
        System.out.println("\f ***DeleteCopyMenu ***\n");
        if(dvdCtr.getCopy(id, serial) != null)
        {
            dvdCtr.deleteCopy(id, serial);
            System.out.println("Copy deleted.");
        }
        else
        {
            System.out.println("Error: Copy not found");
        }
        inputParser.printReturn();
    }

    private void listAllCopies()
    {
        System.out.println("\f ***ListAllCopyMenu ***\n");
        dvdCtr.printCopiesInfo(id);
        inputParser.printReturn();
    }

    private void updateCopy()
    {
        long serial = inputParser.getLong("\f ***UpdateCopyMenu ***\n\nPlease input the serial number of the copy: ");
        if(dvdCtr.getCopy(id, serial) != null)
        {
            int choise = 0;
            String value = "";
            boolean exit = false;
            while(choise > 0 && choise < 4 || !exit)
            {
                choise = inputParser.getInt("\f ***UpdateCopyMenu ***\n\n1. Edit purchase date\n2. Edit purchase price\n3. Return\n\nMake your choise: ");
                if(choise > 0 && choise < 4)
                {
                    if(choise == 3)
                    {
                        choise = 4;
                        exit = true;
                    }
                    else
                    {
                        if(choise == 1)
                        {
                            value = String.valueOf(inputParser.getDate("\f ***UpdateCopyMenu ***\n\nPlease input the purchase date (dd/mm/yyyy) of the copy: "));
                        }
                        else if(choise == 2)
                        {
                            value = String.valueOf(inputParser.getInt("\f ***UpdateCopyMenu ***\n\nPlease input the purchase price of the copy: "));
                        }
                        dvdCtr.updateCopy(id, choise, serial, value);
                        System.out.println("\f ***UpdateCopyMenu ***\n\nCopy updated");
                        inputParser.printReturn();
                    }
                }
            }
        }
        else
        {
            System.out.println("\f ***UpdateCopyMenu ***\n\nError: Copy not found");
            inputParser.printReturn();
        }
    }

}
